package pl.niemar22.unitests;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class sel2 {
	private WebDriver driver;
	private String baseUrl="";
	private StringBuffer verificationErrors = new StringBuffer();
	@Before
	public void setUp() throws Exception {
		driver = new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}

	@Test
	public void test1() throws Exception {
		driver.get("/p/selenium/");
		driver.findElement(By.linkText("Downloads")).click();
		driver.findElement(By.linkText("Wiki")).click();
		driver.findElement(By.linkText("Issues")).click();
		driver.findElement(By.linkText("Source")).click();
		driver.findElement(By.linkText("Project Home")).click();
		driver.findElement(By.linkText("Issues")).click();
		// ERROR: Caught exception [ERROR: Unsupported command [isTextPresent]]
		driver.findElement(By.id("searchq")).click();
		driver.findElement(By.id("searchq")).clear();
		driver.findElement(By.id("searchq")).sendKeys("problem");
		driver.findElement(By.cssSelector("span.inIssueList > form > input[type=\"submit\"]")).click();
		// ERROR: Caught exception [ERROR: Unsupported command [isTextPresent]]
		// ERROR: Caught exception [ERROR: Unsupported command [isTextPresent]]
		// ERROR: Caught exception [ERROR: Unsupported command [isTextPresent]]
		driver.findElement(By.id("pname")).click();
		driver.findElement(By.linkText("Source")).click();
		// ERROR: Caught exception [ERROR: Unsupported command [isTextPresent]]
		driver.findElement(By.linkText("Project Home")).click();
		// ERROR: Caught exception [ERROR: Unsupported command [isTextPresent]]
		driver.findElement(By.linkText("Issues")).click();
		driver.findElement(By.id("searchq")).click();
	}

	private void insertText ( By identifier, String text){
		WebElement element = driver.findElement(identifier);
		element.clear();
		element.sendKeys(text);
	}
	@After
	public void tearDown() throws Exception {
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}

	private boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}
}
